package com.leixin.demo.service.impl;

import com.leixin.demo.dao.UserDao;
import com.leixin.demo.entity.RedisEntity;
import com.leixin.demo.entity.UserEntity;
import com.leixin.demo.service.UserService;
import jdk.nashorn.internal.runtime.FindProperty;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    RedisTemplate redisTemplate;

    private static String url = "http://ip.ws.126.net/ipquery?ip=";

    @Override
    public UserEntity queryUserById(String id) {

        return userDao.queryUserById(id);
    }

    @Override
    public void addUser(UserEntity userEntity) {
        redisTemplate.opsForValue().set(userEntity.getId(), userEntity);
        userDao.addUser(userEntity);
    }

    @Override
    public List<String> get(File file, List<String> list) {
        File[] listFiles = file.listFiles();
        if (listFiles != null && listFiles.length > 0) {
            for (File file2 : listFiles) {
                if (file2.isDirectory()) {
                    get(new File(file2.getAbsolutePath()), list);
                }
                if (file2.isFile()) {
                    String path = file2.getAbsolutePath();
                    if (path.endsWith(".mp4")) {
                        list.add(file2.getAbsolutePath().substring(3));
                    }
                }
            }
        }
        return list;
    }

    @Override
    public String saveComment(String comment, HttpServletRequest request) throws IOException {
        RedisEntity redisEntity = new RedisEntity();
        String ip = request.getHeader("x-forwarded-for");
        if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        String http = url + ip;
        String address = "";
        HttpClient client = new DefaultHttpClient();
        //发送get请求
        HttpGet get = new HttpGet(http);
        HttpResponse response = client.execute(get);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            /**读取服务器返回过来的json字符串数据**/
            address = EntityUtils.toString(response.getEntity());
            char[] chars = address.toCharArray();
            for (int i=0;i<chars.length;i++) {
                if ((chars[i] >= 0x4e00) && (chars[i] <= 0x9fbb)) {
                    sbf.append(chars[i]);
                }
            }
        }
        redisEntity.setComment(comment);
        redisEntity.setTime(new Date());
        redisEntity.setIp(ip);
//        redisEntity.setAddress(address);
        redisTemplate.opsForList().leftPush("comment", redisEntity);
        return "成功";
    }

    @Override
    public List getComment() {
        List list = redisTemplate.opsForList().range("comment", 0, -1);
        return list;
    }

}
